﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TemplateWeb.Models.Repositories;
using TemplateWeb.Helpers;
using TemplateWeb.Models;

namespace TemplateWeb.ShoppingCartModule
{
    public class ShoppingCart
    {
        string ShoppingCartId { get; set; }
        public const string CartSessionKey = "CartId";
        private GioHangRepository _repCart = new GioHangRepository();
        private SanPhamRepository _repItem = new SanPhamRepository();

        public static ShoppingCart GetCart(HttpContextBase context)
        {
            var cart = new ShoppingCart();
            cart.ShoppingCartId = cart.GetCartId(context);
            return cart;
        }

        // Helper method to simplify shopping cart calls 
        public static ShoppingCart GetCart(Controller controller)
        {
            return GetCart(controller.HttpContext);
        }

        public void AddToCart(SanPham item)
        {
            // Get the matching cart and album instances 
            var cartItem = _repCart.SelectByIDAndSanPham(ShoppingCartId, item.ID);

            if (cartItem == null)
            {
                // Create a new cart item if no cart item exists 
                cartItem = new GioHang
                {
                    IDSanPham = item.ID,
                    CartId = ShoppingCartId,
                    SoLuong = 1,
                    NgayTao = DateTime.Now
                };
                // Insert new cart
                _repCart.Insert(cartItem);
            }
            else
            {
                // If the item does exist in the cart, then add one to the quantity 
                cartItem.SoLuong ++;
                // Update cart
                _repCart.Save();
            }
        }

        public void AddToCart(SanPham item, int num)
        {
            // Get the matching cart and album instances 
            var cartItem = _repCart.SelectByIDAndSanPham(ShoppingCartId, item.ID);
            // If the item does exist in the cart, then add one to the quantity 
            cartItem.SoLuong = num;
            // Update cart
            _repCart.Save();
        }

        public bool RemoveFromCart(int recordid)
        {
            // Get the cart 
            // Get the cart 
            return _repCart.Delete(ShoppingCartId, recordid);
        }

        public bool EmptyCart()
        {
            return _repCart.Empty(ShoppingCartId);
        }

        public List<GioHang> GetCartItems()
        {
            var lst = _repCart.SelectByGioHangID(ShoppingCartId);
            return lst;
        }

        public double GetTotal()
        {
            // Multiply album price by count of that album to get  
            // the current price for each of those albums in the cart 
            // sum all album price totals to get the cart total 
            var lstCart = _repCart.SelectByGioHangID(ShoppingCartId);
            double total = 0;
            if (lstCart != null)
            {
                foreach (var item in lstCart)
                {
                    var buyItem = _repItem.SelectByID(item.IDSanPham.Value);
                    total += (double)item.SoLuong * buyItem.GiaBan.Value;
                }
            }
            return Math.Round(total, 2); 
        }

         // We're using HttpContextBase to allow access to cookies. 
        public string GetCartId(HttpContextBase context)
        {
            if (context.Session[CartSessionKey] == null)
            {
                if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
                {
                    context.Session[CartSessionKey] = context.User.Identity.Name;
                }
                else
                {
                    // Generate a new random GUID using System.Guid class 
                    Guid tempCartId = Guid.NewGuid();

                    // Send tempCartId back to client as a cookie 
                    context.Session[CartSessionKey] = tempCartId.ToString();
                }
            }
            return context.Session[CartSessionKey].ToString(); 
        }

        // When a user has logged in, migrate their shopping cart to 
        // be associated with their username 
        public void MigrateCart(string userName)
        {
            var shoppingCart = _repCart.SelectByGioHangID(ShoppingCartId);

            foreach (GioHang item in shoppingCart)
            {
                item.CartId = userName;
            }
        }
    }
}